package com.tencent.cloud.uikit.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.tencent.cloud.uikit.R;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;

/**
 * 系统 UI 工具类
 *
 * @author showxiao
 * @date 2018/7/3
 */
public class SystemUiUtils {

    /**
     * 沉浸式全屏模式
     *
     * 状态栏背景透明，内容区域全屏显示
     *
     * @param activity
     */
    public static void translucent(@NonNull Activity activity) {
        QMUIStatusBarHelper.translucent(activity);
        if (activity instanceof AppCompatActivity) {
            ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
            if (actionBar != null) {
                actionBar.hide();
            }
        }
    }

    /**
     * 显示标题栏：透明背景
     *
     * @param activity
     * @param title     标题文本
     * @return
     */
    public static IovToolbar showActionBar(final Activity activity,
                                           final CharSequence title) {
        return showActionBar(activity, title, null);
    }

    /**
     * 显示标题栏：透明背景
     *
     * @param activity
     * @param title         标题文本
     * @param background    标题栏背景
     * @return
     */
    public static IovToolbar showActionBar(final Activity activity,
                                           final CharSequence title,
                                           final Drawable background) {
        return showActionBar(activity, title, background, false);
    }

    /**
     * 显示标题栏
     *
     * @param activity
     * @param title                 标题文本
     * @param background            标题栏背景
     * @param closeOnRight          关闭按钮置于右边
     * @return
     */
    public static IovToolbar showActionBar(final Activity activity,
                                           final CharSequence title,
                                           final Drawable background,
                                           final boolean closeOnRight) {
        if (activity == null || !(activity instanceof AppCompatActivity)) {
            return null;
        }

        final ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
        if (actionBar == null) {
            return null;
        }

        // 配置显示自定义布局
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);

        // 创建自定义布局
        final IovToolbar iovToolbar = new IovToolbar(activity);
        iovToolbar.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        ));
        // 设置标题
        iovToolbar.setTitle(title);
        // 设置关闭按钮在右边显示
        iovToolbar.setCloseOnRight(closeOnRight);
        // 设置返回事件
        iovToolbar.setHomeAsUpListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });

        // 设置自定义布局
        actionBar.setCustomView(iovToolbar);
        // 清除左右外边距
        Toolbar parent = (Toolbar) iovToolbar.getParent();
        if (parent != null) {
            parent.setPadding(0,0,0,0);
            parent.setContentInsetsAbsolute(0,0);
        }

        // 设置透明背景
        actionBar.setBackgroundDrawable(background);
        // 显示标题栏
        actionBar.show();

        return iovToolbar;
    }

    public static void hideActionBar(AppCompatActivity activity) {
        if (activity != null && activity.getSupportActionBar() != null) {
            activity.getSupportActionBar().hide();
        }
    }

    public static int getActionBarHeight(Context context) {
        return QMUIDisplayHelper.getActionBarHeight(context);
    }

    public static int getStatusBarHeight(Context context) {
        return QMUIStatusBarHelper.getStatusbarHeight(context);
    }

    public static int getThemeResId(Context context) {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
        return outValue.resourceId;
    }
}
